/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.service.impl;

import me.zhengjie.modules.anniversary.domain.GzmuAnnAboutus;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.anniversary.repository.GzmuAnnAboutusRepository;
import me.zhengjie.modules.anniversary.service.GzmuAnnAboutusService;
import me.zhengjie.modules.anniversary.service.dto.GzmuAnnAboutusDto;
import me.zhengjie.modules.anniversary.service.dto.GzmuAnnAboutusQueryCriteria;
import me.zhengjie.modules.anniversary.service.mapstruct.GzmuAnnAboutusMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yangyao
* @date 2020-09-25
**/
@Service
@RequiredArgsConstructor
public class GzmuAnnAboutusServiceImpl implements GzmuAnnAboutusService {

    private final GzmuAnnAboutusRepository gzmuAnnAboutusRepository;
    private final GzmuAnnAboutusMapper gzmuAnnAboutusMapper;

    @Override
    public Map<String,Object> queryAll(GzmuAnnAboutusQueryCriteria criteria, Pageable pageable){
        Page<GzmuAnnAboutus> page = gzmuAnnAboutusRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(gzmuAnnAboutusMapper::toDto));
    }

    @Override
    public List<GzmuAnnAboutusDto> queryAll(GzmuAnnAboutusQueryCriteria criteria){
        return gzmuAnnAboutusMapper.toDto(gzmuAnnAboutusRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public GzmuAnnAboutusDto findById(Integer id) {
        GzmuAnnAboutus gzmuAnnAboutus = gzmuAnnAboutusRepository.findById(id).orElseGet(GzmuAnnAboutus::new);
        ValidationUtil.isNull(gzmuAnnAboutus.getId(),"GzmuAnnAboutus","id",id);
        return gzmuAnnAboutusMapper.toDto(gzmuAnnAboutus);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GzmuAnnAboutusDto create(GzmuAnnAboutus resources) {
        return gzmuAnnAboutusMapper.toDto(gzmuAnnAboutusRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(GzmuAnnAboutus resources) {
        GzmuAnnAboutus gzmuAnnAboutus = gzmuAnnAboutusRepository.findById(resources.getId()).orElseGet(GzmuAnnAboutus::new);
        ValidationUtil.isNull( gzmuAnnAboutus.getId(),"GzmuAnnAboutus","id",resources.getId());
        gzmuAnnAboutus.copy(resources);
        gzmuAnnAboutusRepository.save(gzmuAnnAboutus);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            gzmuAnnAboutusRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<GzmuAnnAboutusDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GzmuAnnAboutusDto gzmuAnnAboutus : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("关于我们详情", gzmuAnnAboutus.getText());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
